package com.best.core.utils;

import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import com.best.core.exception.CommonException;
import com.best.core.exception.CommonExceptionEnum;

/**
 * ClassName: GenerateToken Package: com.best.core.utils Description: 生成token工具类 Datetime: 2020/6/12
 * 16:20 Author: nike
 */
@Component
public class GenerateToken {
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 生成令牌
     * 
     * @param keyPrefix 牌key前缀
     * @param redisValue redis存放的值
     * @return 返回token
     */
    public String createToken(String keyPrefix, String redisValue) {
        return createToken(keyPrefix, redisValue, null);
    }

    /**
     * 生成令牌
     *
     * @param keyPrefix 令牌key前缀
     * @param redisValue redis存放的值
     * @param time 有效期
     * @return 返回token
     */
    public String createToken(String keyPrefix, String redisValue, Long time) {
        if (StringUtils.isEmpty(redisValue)) {
            throw new CommonException(CommonExceptionEnum.REDIS_VALUE_IS_NULL,
                    "redis value is null !");
        }
        String token = keyPrefix + "." + UUID.randomUUID().toString().replace("-", "");
        redisUtil.setString(token, redisValue, time);
        return token;
    }

    /**
     * 根据token获取redis中的value值
     *
     * @param token
     * @return
     */
    public String getToken(String token) {
        if (StringUtils.isEmpty(token)) {
            return null;
        }
        String value = redisUtil.getString(token);
        return value;
    }

    /**
     * 移除token
     *
     * @param token
     * @return
     */
    public Boolean removeToken(String token) {
        if (StringUtils.isEmpty(token)) {
            return null;
        }
        // return redisUtil.delKey(token);
        StringRedisTemplate stringRedisTemplate = redisUtil.getStringRedisTemplate();
        Boolean rs = stringRedisTemplate.delete(token);
        return rs;
    }

}
